//
//  KMenuView.m
//  Keping
//
//  Created by haha on 2017/7/13.
//  Copyright © 2017年 柯平. All rights reserved.
//

#import "KMenuView.h"

#pragma mark -  KDropDownMenuViewCell

@interface KDropDownMenuViewCell : UITableViewCell

//@property (nonatomic, strong) UIImageView *iconImageView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *numLabel;

@end

@implementation KDropDownMenuViewCell

+ (BOOL)requiresConstraintBasedLayout
{
    return YES;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        UILabel *titleLabel = [[UILabel alloc] init];
        titleLabel.font = [UIFont systemFontOfSize:14];
        titleLabel.textColor = RGBCOLOR(22, 33, 75);
        titleLabel.textAlignment = NSTextAlignmentCenter;
        self.titleLabel = titleLabel;
        [self.contentView addSubview:titleLabel];
        
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.mas_equalTo(0);
            make.bottom.mas_equalTo(1);
        }];
        
        UILabel *numlabel = [[UILabel alloc] init];
        numlabel.font = [UIFont systemFontOfSize:14];
        numlabel.textColor = kWhiteColor;
        numlabel.textAlignment = NSTextAlignmentCenter;
        numlabel.backgroundColor = RGBCOLOR(250, 82, 30);
        numlabel.layer.masksToBounds = YES;
        numlabel.layer.cornerRadius = 10.0;
        numlabel.layer.borderWidth = 1.0;
        numlabel.layer.borderColor = kGrayColor.CGColor;
        self.numLabel = numlabel;
        [self.contentView addSubview:numlabel];
        
        [self.numLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.mas_centerY);
            make.right.equalTo(self.mas_right).offset(-8);
            make.height.width.mas_offset(20);
        }];
        
        //下划线
        UIView *line = [[UIView alloc]init];
        line.backgroundColor = [UIColor colorWithHexString:@"ededed"];
        [self.contentView addSubview:line];
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView).offset(15);
            make.right.equalTo(self.contentView).offset(-15);
            make.bottom.equalTo(self.contentView);
            make.height.equalTo(@1);
        }];

    }
    return self;
}

@end


@interface KMenuView() <UITableViewDataSource,UITableViewDelegate>

/** 容器  */
@property (nonatomic, strong) UIImageView * containerView;
/** tabelView */
@property (nonatomic, strong) UITableView *tableView;
/**   */
@property (nonatomic, weak) UIView *bgView;
/** 数据  */
@property (nonatomic, strong) NSArray *dictArray;
/** block  */
@property (nonatomic, copy) didSelectRowOfIndexPathBlock selectRowBlock;

@property (nonatomic, strong) UIButton *clearColorBtnBackView;

@end

@implementation KMenuView


-(instancetype)initWithFrame:(CGRect)frame itemDictArray:(NSArray *)dictArray
{
    if (self = [super initWithFrame:frame])
    {
        _dictArray = dictArray;
        _containerView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        //需要图片处理
        UIImage *oldImage = [UIImage imageNamed:@"add_bg.9"];
//        UIImage *newImage = [oldImage stretchableImageWithLeftCapWidth:oldImage.size.width * 0.18 topCapHeight:oldImage.size.height * 0.6];
        _containerView.image = oldImage;
        _containerView.alpha = 1.0;
        _containerView.contentMode = UIViewContentModeScaleToFill;
        self.clipsToBounds = YES;
        [self addSubview:_containerView];
        [self addSubview:self.tableView];
        self.backgroundColor = [UIColor clearColor];
        
    }
    
    return self;
}

-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(5, 10, self.width-10, self.height-15) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.alwaysBounceVertical = NO;
        _tableView.showsVerticalScrollIndicator = YES;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor clearColor];
    }
    
    return _tableView;
}

#pragma mark - TableviewDatasource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dictArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ID = @"KDropDownMenuViewCellID";
    KDropDownMenuViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[KDropDownMenuViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor clearColor];
    }
    
    NSDictionary * rowDict = self.dictArray[indexPath.row];
//    cell.iconImageView.image = [UIImage imageNamed:rowDict[@"icon"]];
    cell.titleLabel.text = rowDict[@"title"];
    cell.numLabel.text = rowDict[@"Num"];
    if ([rowDict[@"Num"] isEqualToString:@"0"]) {
        cell.numLabel.hidden = YES;
    } else {
        cell.numLabel.hidden = NO;
    }
    return cell;
}

#pragma mark Cell点击事件处理器

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    !_selectRowBlock?:_selectRowBlock(indexPath);
    [self dismiss];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return (self.height - 15)/(CGFloat)self.dictArray.count;
}


-(void)showWithDidSelectRowOfIndexPathBlock:(didSelectRowOfIndexPathBlock)selectRowOfIndexPathBlock
{
    _selectRowBlock = selectRowOfIndexPathBlock;
    
    CGRect frame = self.frame;
    
    UIWindow *window = [UIApplication sharedApplication].keyWindow;

    UIView *bgView = [[UIView alloc]init];
    bgView.frame = CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT);
    [bgView setBackgroundColor:[UIColor colorWithWhite:0 alpha:0.3]];
    [window addSubview:bgView];
    self.bgView = bgView;
    
    self.clearColorBtnBackView = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.clearColorBtnBackView setFrame:[UIScreen mainScreen].bounds];
    [self.clearColorBtnBackView setBackgroundColor:[UIColor clearColor]];
    [self.clearColorBtnBackView addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
    [window addSubview:self.clearColorBtnBackView];
    self.height = 0;

    [self.clearColorBtnBackView addSubview:self];
    [UIView animateWithDuration:0.2f animations:^{
        self.height = frame.size.height;
    }];
}


- (void)dismiss
{
    
    [UIView animateWithDuration:0.2f animations:^{
        self.height = 0;
    } completion:^(BOOL finished) {
        [self.clearColorBtnBackView removeFromSuperview];
        [self.bgView removeFromSuperview];
    }];
}


@end

